package org.dcits.controller;



import javax.servlet.http.HttpSession;

import org.dcits.dao.UserDao;
import org.dcits.model.Userinfo;
import org.dcits.util.Md5Util;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;





@Controller
public class UserController {

	@RequestMapping("/")
	public String tologin() {
		return "login";
	}
	
	@RequestMapping("/login")
	@ResponseBody
	public String login(@RequestParam("username") String userName,@RequestParam("password") String userPwd,HttpSession hs) {
		Userinfo user=new Userinfo();
		user.setUserName(userName);
		user.setUserPwd(Md5Util.getMd5(userPwd));
		boolean result=UserDao.login(user);
		if(result) {
			int userId=UserDao.findIdByUname(userName);
			hs.setAttribute("userId", userId);
			hs.setAttribute("userName", userName);
			String acctNo=UserDao.findCardNoByUid(userId);
			hs.setAttribute("acctNo", acctNo);
			return "success"; 
		}
		else
		return "fail";
	}
	
	@RequestMapping("/tomain")
	public String toindex() {
		return "index";
	}
	@RequestMapping("/toregit")
	public String toregit() {
		return "regist";
	}
	@RequestMapping("/regist")
	@ResponseBody
	public String regist(Userinfo user) {
		user.setUserPwd(Md5Util.getMd5(user.getUserPwd()));
		System.out.println(user);
		boolean result=UserDao.regist(user);
		System.out.println(result);
		if(result)
			return "success"; 
		else
		return "fail";

	}
	@RequestMapping("/uNameIsExist")
	@ResponseBody
	public String uNameIsExist(@RequestParam("userName") String userName) {
		
		System.out.println(userName);
		Boolean result=UserDao.uNameIsExist(userName);
		if(result)
			return "true"; 
		else
		return "false";
	}
	
}
